<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="4">
              <a-form-item label="姓名">
                <a-input
                  v-model="queryParam.cusName"
                  placeholder="请输入客户姓名"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                />
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="是否逾期">
                <a-select v-model="queryParam.isOverdue" allow-clear>
                  <a-select-option key="0" value="0">否</a-select-option>
                  <a-select-option key="1" value="1">是</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />刷新</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-loan-bill-add-form
        v-if="showAddModal"
        ref="bizLoanBillAddForm"
        :loanTypeOptions="loanTypeOptions"
        :isOverdueOptions="isOverdueOptions"
        :isPayAllOptions="isPayAllOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-loan-bill-edit-form
        v-if="showEditModal"
        ref="bizLoanBillEditForm"
        :loanTypeOptions="loanTypeOptions"
        :isOverdueOptions="isOverdueOptions"
        :isPayAllOptions="isPayAllOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        :loading="loading"
        rowKey="id"
        :columns="columns"
        :expandedRowKeys="expandedKeys"
        @expand="onExpandCurrent"
        :data-source="bizLoanBillList"
        title="还款信息"
        size="middle"
        tableKey="biz-loanBill-index-table"
        @refresh="getList"
        :format-conditions="true"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        :scroll="{ x: '100vh' }"
      >
        <span slot="expandedRowRender" slot-scope="{ record }">
          <detail-index
            ref="detailIndex"
            @getlist="resetQuery"
            :title="'还款计划（' + record.months + '期）'"
            :loanId="record.id"
          />
        </span>
        <span slot="loanType" slot-scope="{ record }">
          {{ loanTypeFormat(record) }}
        </span>

        <span slot="isOverdue" slot-scope="{ text, record }">
          <template v-if="record.isOverdue == '1'">
            <a-badge status="error" />已逾期
          </template>
          <template v-else> <a-badge status="processing" />未逾期 </template>
        </span>

        <span slot="isPayAll" slot-scope="{ record }">
          <template v-if="record.isPayAll == '1'">
            <a-badge status="success" />已结清
          </template>
          <template v-else> <a-badge status="processing" />未结清 </template>
        </span>

        <span slot="operation" slot-scope="{ text, record }">
          <a
            @click="handleUpdate(record)"
            v-hasPermi="['biz:bizLoanBill:edit']"
          >
            修改
          </a>
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import {
  listBizLoanBill,
  delBizLoanBill,
  exportBizLoanBill,
} from "@/api/biz/bizLoanBill";

import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizLoanBillAddForm from "@/views/biz/loanbill/modules/BizLoanBillAddForm";
import BizLoanBillEditForm from "@/views/biz/loanbill/modules/BizLoanBillEditForm";
import DetailIndex from "./DetailIndex";
export default {
  name: "BizLoanBill",
  components: {
    AdvanceTable,
    BizLoanBillAddForm,
    BizLoanBillEditForm,
    DetailIndex,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 还款信息表格数据
      bizLoanBillList: [],
      // 贷款方式字典
      loanTypeOptions: [],
      // 是否逾期字典
      isOverdueOptions: [],
      // 是否结清字典
      isPayAllOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
      },
      columns: [
        {
          title: "进件编号",
          dataIndex: "jinjianId",
          align: "center",
          width: "18%",
        },
        {
          title: "客户姓名",
          dataIndex: "cusName",
          align: "center",
          width: "10%",
        },
        {
          title: "本金金额",
          dataIndex: "principal",
          align: "right",
          width: "10%",
        },
        {
          title: "贷款月数",
          dataIndex: "months",
          align: "right",
          width: "10%",
        },
        {
          title: "贷款方式",
          dataIndex: "loanType",
          scopedSlots: { customRender: "loanType" },
          align: "center",
          width: "10%",
        },
        {
          title: "还款开始日期",
          dataIndex: "beginTime",
          align: "center",
          width: "10%",
        },

        {
          title: "到期日期",
          dataIndex: "endTime",
          align: "center",
          width: "10%",
        },
        // {
        //   title: "还款日",
        //   dataIndex: "payDate",
        //   align: "center",
        //   width: "10%",
        // },

        {
          title: "年利率（%）",
          dataIndex: "lv",
          align: "right",
          width: "10%",
        },
        {
          title: "月供金额",
          dataIndex: "monthPayment",
          align: "right",
          width: "10%",
        },
        {
          title: "总还款额",
          dataIndex: "totalPayment",
          align: "right",
          width: "10%",
        },
        {
          title: "总利息",
          dataIndex: "totalInterest",
          align: "right",
          width: "10%",
        },
        {
          title: "是否逾期",
          dataIndex: "isOverdue",
          scopedSlots: { customRender: "isOverdue" },
          align: "center",
          width: "10%",
        },
        {
          title: "是否结清",
          dataIndex: "isPayAll",
          scopedSlots: { customRender: "isPayAll" },
          align: "center",
          width: "10%",
        },
        {
          title: "逾期天数",
          dataIndex: "overdueDays",
          align: "right",
          width: "10%",
        },
        {
          title: "逾期说明",
          dataIndex: "overdueRemark",
          ellipsis: true,
          align: "left",
          width: "10%",
        },
        {
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "10%",
          scopedSlots: { customRender: "operation" },
        },
      ],
      expandedKeys: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("back_type").then((response) => {
      this.loanTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.isOverdueOptions = response.data;
      console.log(this.isOverdueOptions);
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.isPayAllOptions = response.data;
    });
  },
  methods: {
    onExpandCurrent(expandedKeys, row) {
      if (expandedKeys) {
        this.expandedKeys = [row.id];
      } else {
        this.expandedKeys = [];
      }
    },
    /** 查询还款信息列表 */
    getList() {
      this.loading = true;
      listBizLoanBill(this.queryParam).then((response) => {
        this.bizLoanBillList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 贷款方式字典翻译
    loanTypeFormat(row) {
      if (row.loanType) {
        return this.selectDictLabel(this.loanTypeOptions, row.loanType);
      } else {
        return "";
      }
    },
    // 是否逾期字典翻译
    isOverdueFormat(row) {
      if (row.isOverdue) {
        return this.selectDictLabel(this.isOverdueOptions, row.isOverdue);
      } else {
        return "";
      }
    },
    // 是否结清字典翻译
    isPayAllFormat(row) {
      if (row.isPayAll) {
        return this.selectDictLabel(this.isPayAllOptions, row.isPayAll);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizLoanBillAddForm.handleAdd());
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizLoanBillEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizLoanBillIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizLoanBill(bizLoanBillIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizLoanBill(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
